import axios from 'axios';
import { showToast } from './utils/utils';
import { encrypt, generateRandomString, getCurrentTimestamp, insertAtPosition } from './utils/rsaUtils';
const api = axios.create({
  // baseURL: '/api',
  // baseURL: 'http://sys.zhisuoyi.net:8181',
  baseURL: 'https://sys.zhisuoyi.net/prod-api/',
  timeout: 120000,
});

// 提交订单-图片上传

export async function apiOssUpload(file: File) {
  const formData = new FormData();
  formData.append('file', file);
  let res = await api.post('/agent/prdTerminal/uploadData', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });

  let url: string | null = null;
  if (res.data.code === 200) {
    url = res.data.data.url;
  } else {
    showToast(res.data.msg ?? '上传失败');
  }

  return url;
}
// 获取换卡订单详细信息
export async function apiCardDetailConvertOrder(id: string) {
  return await api.get(`/agent/prdCardConver/clientGetInfo/${id}`);
}
// 换卡订单支付
export async function apiCardDetailConvertOrderPay(data: any) {
  return await api.post('/agent/prdCardConver/pay', data);
}
// 解密
export async function apiDecryptParam(params: any) {
  let res = await api.get('/agent/prdCardConver/decryptParam', { params });
  return res;
}
// 获取openId
export async function apiCodeToOpenId(code: string, appId: string, url: string) {
  let res = await api.get(`/agent/prdSpread/getWxAccessToken?code=${code}&appId=${appId}&url=${url}`);
  return res;
}
// 发送验证码
export async function apiGetSmsCodeNewEncrypt(params: any) {
  const timestamp = getCurrentTimestamp();
  const str = encrypt(params.phone + timestamp) || '';
  const randomStr = generateRandomString(4);
  const NewStr = insertAtPosition(str, randomStr, 11);
  params.phone = NewStr;
  let res = await api.get(`/agent/prdTerminal/getSmsCodeNewEncrypt?`, { params });
  return res;
}

// 客服链接
export async function apiGetServerLink(params: any) {
  let res = await api.get('/agent/prdCardConver/getConverCustomerUrl', { params });
  return res;
}
